package com.software.chemistry.controller;

import com.software.chemistry.constant.APiConstants;
import com.software.chemistry.domain.Result;
import com.software.chemistry.entity.StudentInfo;
import com.software.chemistry.entity.TeacherInfo;
import com.software.chemistry.entity.UserQuery;
import com.software.chemistry.service.TeacherInfoService;
import com.software.chemistry.util.ImageFileUtils;
import com.software.chemistry.util.ResultUtil;
import com.software.chemistry.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/teacher_info")
public class TeacherInfoController {
    @Autowired
    private TeacherInfoService teacherInfoService;

    //根据Id来获取用户名
    @GetMapping("findTeacherInfoByuserId/{userId}")
    public TeacherInfo findTeacherInfoByuserId(@PathVariable String userId) {
        TeacherInfo str = teacherInfoService.findTeacherInfoByuserId(userId);
        return str;
    }
    //搜索联系人

//    //测试
//    @GetMapping("findTeacherInfos")
//    public List<TeacherInfo> findTeacherInfos(){
//        List<TeacherInfo> teacherInfos = teacherInfoService.findTeacherInfos() ;
//        return teacherInfos ;
//    }


    @PostMapping("/createTeacherInfo")
    private Result<TeacherInfo> createTeacherInfo(@RequestParam("param_username") String param_username, @RequestParam("param_password") String param_password, @RequestParam("file") MultipartFile file, TeacherInfo teacherInfo) {
        teacherInfo.setUserId(UUID.randomUUID().toString());
        teacherInfo.setUserName(param_username);
        teacherInfo.setPassword(param_password);
        String icon = "";
        if (file != null) {
            icon = ImageFileUtils.loadImg(file);
        }
        teacherInfo.setIcon(APiConstants.BASE_URL + icon);
        teacherInfoService.createTeacherInfo(teacherInfo);
        return ResultUtil.success(teacherInfo);
    }


    @PostMapping("/checkTeacherInfoByUserName")
    private Result<TeacherInfo> findTeacherInfoByUserName(@RequestParam("param_username") String param_username, TeacherInfo teacherInfo) {
        if (teacherInfoService.findTeacherInfoByUserName(param_username).size() > 0) {
            return ResultUtil.error("1", "该用户名已存在", teacherInfo);
        }
        return ResultUtil.success(teacherInfo);
    }

    //验证用户名密码是否正确
    @PostMapping("/findTeacherInfoByUserName")
    private Result<TeacherInfo> findTeacherInfoByUserName(@RequestParam("param_username") String param_username, @RequestParam("param_password") String param_password) {

        List<TeacherInfo> teacherInfo = teacherInfoService.verifyLoginUser(param_username, param_password);
        if (teacherInfo.size() > 0) {
            return ResultUtil.success(teacherInfo);
        } else
            return ResultUtil.error("2", "用户名密码不匹配", teacherInfo);
    }

    //根据用户名或者用户id查询
    @GetMapping("findTeacherInfoSByUserNameOrUserId/{str}")
    private Result<UserQuery> findTeacherInfoSByUserNameOrUserId(@PathVariable String str) {
        List<UserQuery> userQueries = teacherInfoService.findTeacherInfoSByUserNameOrUserId(str, str);
        if (userQueries.size() > 0) {
            return ResultUtil.success(userQueries);
        } else {
            return ResultUtil.error("2", "没找到", userQueries);
        }
    }

}
